var is_scolll = false;
var widths = document.documentElement.clientWidth;

window.ontouchmove = function(e) {
	try {
		if (is_scolll) {
			e.preventDefault && e.preventDefault();
			e.returnValue = false;
			e.stopPropagation && e.stopPropagation();
			uexWindow.setBounce("0");
			document.body.style.overflowX = document.body.style.overflowY = "hidden";
			return false;
		} else {
			uexWindow.setBounce("1");
			document.body.style.overflowX = document.body.style.overflowY = "auto";
		}
	} catch (e)  {}

}

/**
 * 拖动产品删除动画
 * @param {Object} dragId 拖动ID号
 * @param {Object} webKieId 拖动层ID号
 * @param {Object} box 里面层box 属性值
 * @param {Object} begin_call 触发滑动的函数。
 * @param {Object} end_call 滑动结束触发的函数。(1表示已切换 0表示切换失败)
 */
Zm_dragShowPrdocut = function(dragId, webKieId, box, begin_call, end_call) {
	var self = this;

	self.element = document.getElementById(dragId);
	self.begin_call = begin_call;
	self.end_call = end_call;
	self.wekite = document.getElementById(webKieId);
	self.boxWidth = document.getElementById(box).offsetWidth;
	self.element.addEventListener($E.start, self, false);
	self.element.addEventListener($E.move, self, false);
	self.element.addEventListener($E.end, self, false);

	if ($E.cancel != "") {
		self.element.addEventListener($E.cancel, self, false);
		//		document.addEventListener($E.cancel, self, false);
	}
	self.element.addEventListener($E.end, self, false);
	self.touchEnabled = true;
	self.wekite.setAttribute("currentX", 0);
	self.firePageX = 0;
	self.move = false;
}

Zm_dragShowPrdocut.prototype = {
	handleEvent: function(event) {
		var self = this;
		switch (event.type) {
			case $E.start:
				self._touchStart(event);
				break;
			case $E.move:
				self._touchMove(event);
				break;
			case $E.end:
			case $E.cancel:
				self._touchEnd(event);
				break;
		}
	},

	_touchStart: function(event) {
		var self = this;
		if (self._locked) {
			event.preventDefault();
			event.stopPropagation();
		}

		if (!self.touchEnabled) {
			return;
		}

		if (!$support.touch) {
			event.preventDefault();
		}

		if (self.begin_call) self.begin_call(self);
		self.startPageX = getPage(event, 'pageX');
		self.startPageY = getPage(event, 'pageY');
		self.basePage = self.startPageX;
		self.wekite.style.webkitTransitionDuration = '0';
		self.moveReady = false;
		self.currentXY = parseInt(self.wekite.getAttribute("currentX"));
		console.log("开始出发");
	},
	_touchMove: function(event) {
		var self = this;
		if (self._locked) {
			event.preventDefault();
			event.stopPropagation();
		}

		
		var pageX = getPage(event, 'pageX'),
			pageY = getPage(event, 'pageY'),
			distXY, newXY;
		
		if (!self.moveReady) {
			deltaX = Math.abs(pageX - self.startPageX);
			if (deltaX > 3) {
				if (self._vendor) //Android 4.0 need prevent default
				{
					event.preventDefault();
				}
				self.moveReady = true;
			}
		}
		
		//第一次进入
		if(self.firePageX == 0) {
			if( Math.abs(pageX -self.startPageX) > 10 && Math.abs(pageY -self.startPageY) < 20){		
				self.moveReady = true;
				self.firePageX = 1;	
			}  else {
				self.moveReady = false;
			}
		}

//$toast(parseInt(widths/8)+"|"+Math.abs(pageX -self.startPageX)+"|"+Math.abs(pageY -self.startPageY),500);

		if (self.moveReady) {
			is_scolll = true;
			distXY = pageX - self.basePage;
			newXY = parseInt(self.currentXY + parseInt(distXY));

			self.currentXY = newXY;
			self.basePage = pageX;

			if (newXY > 0) {
				self.wekite.setAttribute("currentX", 0);
				self.wekite.style.webkitTransform = ZmgetTranslateX(0);
			} else {
				if (Math.abs(newXY) <= self.boxWidth) {
					self.wekite.setAttribute("currentX", newXY);
					self.wekite.style.webkitTransform = ZmgetTranslateX(newXY);
				}
			}

		}



	},
	_touchEnd: function(event) {
		var self = this;

		if (self._locked) {
			event.preventDefault();
			event.stopPropagation();
		}

		//模拟弹回去
		if (self.currentXY <= 0 && Math.abs(self.currentXY) > self.boxWidth / 2) {
			self.moveToPoint(-self.boxWidth);
			if (self.end_call) self.end_call(1, self);
		} else {
			self.moveToPoint(0);
			if (self.end_call) self.end_call(0, self);
		}
		is_scolll = false;
		self.firePageX=0;
		console.log("结束");

	},
	moveToPoint: function(point) {
		var self = this;
		self.wekite.setAttribute("currentX", point);
		self.wekite.style.webkitTransitionDuration = '150ms';
		self.wekite.style.webkitTransform = ZmgetTranslateX(point);
		var ev = document.createEvent('Event');
		ev.initEvent('css3flip.moveend', true, false);
		self.element.dispatchEvent(ev);
	},
	destroy: function() {
		var self = this;
		self.element.parentNode.removeEventListener($E.start, self);
		self.element.parentNode.removeEventListener($E.move, self);
		self.element.parentNode.removeEventListener($E.transEnd, self);
		self.element.parentNode.removeEventListener($E.cancel, self);
		document.removeEventListener($E.end, self);
		document.removeEventListener($E.cancel, self);
	}

}

/**
 * 关闭之前的属性
 */
function Zm_dragShowPrdocut_Hide(className, self) {

	classDomName = document.getElementsByClassName(className);
	id = self.wekite.getAttribute("id");
	for (i = 0; i < classDomName.length; i++) {
		if (!self || id != classDomName[i].getAttribute("id")) {
			classDomName[i].style.webkitTransitionDuration = "150ms";
			classDomName[i].style.webkitTransform = ZmgetTranslateX(0);
			classDomName[i].setAttribute("currentX", 0);
		}
	}

}



/**
 * 滑动效果
 */
function ZmgetTranslateX(x) {
	return $support.transform3d ? 'translate3d(' + x + 'px, 0, 0)' : 'translate(' + x + 'px, 0)';
}
